﻿using System;
using System.Collections.Generic;
using System.Text;

using umbraco.cms.presentation.Trees;
using umbraco.interfaces;

namespace Our.Umbraco.Tree.Elmah
{
	/// <summary>
	/// Loads the application tree for ELMAH
	/// </summary>
	public class LoadElmahTree : BaseTree
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="LoadElmahTree"/> class.
		/// </summary>
		/// <param name="application">The application.</param>
		public LoadElmahTree(string application)
			: base(application)
		{
		}
		
		/// <summary>
		/// Renders the specified application tree.
		/// </summary>
		/// <param name="tree">The application tree.</param>
		public override void Render(ref XmlTree tree)
		{
		}

		/// <summary>
		/// Renders the JS.
		/// </summary>
		/// <param name="Javascript">The javascript.</param>
		public override void RenderJS(ref StringBuilder Javascript)
		{
			Javascript.Append(@"function ViewExceptions() { parent.right.document.location.href = '../packages/ELMAH/ViewExceptions.aspx' }");
		}

		/// <summary>
		/// Creates the root node.
		/// </summary>
		/// <param name="rootNode">The root node.</param>
		protected override void CreateRootNode(ref XmlTreeNode rootNode)
		{
			rootNode.Action = "javascript:ViewExceptions();";
			rootNode.HasChildren = false;
			rootNode.Text = "ELMAH";
			rootNode.Icon = "../../../packages/ELMAH/bug.png";
			rootNode.OpenIcon = rootNode.Icon;
			rootNode.NodeID = "init";
			rootNode.NodeType = string.Concat(rootNode.NodeID, this.TreeAlias);
			rootNode.Menu = new List<IAction>();
			rootNode.Source = string.Empty;
		}
	}
}
